package day04.actor

import akka.actor.{Actor, ActorRef, ActorSystem, Props}
import akka.actor.Actor.Receive
import com.typesafe.config.ConfigFactory

/**
  *
  */
class Master extends Actor{
  //用来接收消息的
  override def receive: Receive = {
    case "hello" =>{
      println("a hello msg received")
    }
    case "bye" => {
      println("bye bye")
    }
    case "connect" =>{
      println("a connect msg received from worker")
      //然后再想worker回发一个消息
      //通过sender就可以将消息发送回去
      sender() ! "success"
    }
  }
}

object Master{
  def main(args: Array[String]): Unit = {
    val host = "127.0.0.1"
    val port ="8888"

    val confStr =
      s"""
        |akka.actor.provider = "akka.remote.RemoteActorRefProvider"
        |akka.remote.netty.tcp.hostname = $host
        |akka.remote.netty.tcp.port = $port
      """.stripMargin

    val config = ConfigFactory.parseString(confStr)
    // 创建 actorSystem(单例)
    val actorSystem = ActorSystem("Master_actor_system",config)

    //通过创建的actorSystem 在创建actor
    val masterActor: ActorRef = actorSystem.actorOf(Props[Master](),"Master")


    //发送消息（给自己发）
    masterActor.!("hello")



  }
}
